<template>
  <div class="box">
    <div class="xbox">
      <!-- <el-radio-group v-model="labelPosit;ion" size="small"></el-radio-group> -->
      <div style="margin: 20px;"></div>
      <el-form label-width="80px" :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="用户名:" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
      </el-form>
      <el-row>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-row>
    </div>
  </div>
</template>

<script>
import { login_req } from "@/api/login";
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(ruleForm) {
      console.log(this.$refs);
      this.$refs[ruleForm].validate(async validate => {
        console.log(validate);
        if (validate) {
          // 收集数据
          console.log(this.ruleForm);
          const req = {
            method: "post",
            url: "login",
            data: this.ruleForm,
          };
          let result = await login_req(req);
          console.log(result);
          if (result.meta.status == 200) {
            console.log("跳转到主页");

            // token存放到sessionStorage中
            window.sessionStorage.setItem("token", result.data.token);
            this.$router.push({
              path: "/home"
            });
          }
        }
      });
    },
    resetForm(ruleForm) {
      console.log(ruleForm);
      this.$refs[ruleForm].resetFields();
    },
  }

};
</script>
 
<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100%;
  background-image: url("../img/hb.jpg");
  background-size: 100%;
  margin: 0px auto;
  display: flex;
  justify-content: center;
  align-items: center;
  .xbox {
    width: 500px;
    height: 500px;
    background-color: azure;
  }
}
</style>